All files / api/controllers UserController.js

0% Statements 0/39
0% Branches 0/8
0% Functions 0/5
0% Lines 0/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                                                                                                                                                                 
const GetMeQuery = require('../../application/user/queries/GetMeQuery');
const GetAllUsersQuery = require('../../application/user/queries/GetAllUsersQuery');
const GetUserByIdQuery = require('../../application/user/queries/GetUserByIdQuery');
const UpdateUserProfileCommand = require('../../application/user/commands/UpdateUserProfileCommand');
 
/**
 * User Controller
 * User-related endpoints using CQRS pattern (protected by JWT)
 */
class UserController {
  constructor(getMeQueryHandler, getAllUsersQueryHandler, getUserByIdQueryHandler, updateUserProfileCommandHandler) {
    this.getMeQueryHandler = getMeQueryHandler;
    this.getAllUsersQueryHandler = getAllUsersQueryHandler;
    this.getUserByIdQueryHandler = getUserByIdQueryHandler;
    this.updateUserProfileCommandHandler = updateUserProfileCommandHandler;
  }
 
  /**
   * GET /api/users/me - Bejelentkezett user adatai (protected)
   */
  async getMe(req, res) {
    try {
      // req.user-t az authMiddleware tölti ki a JWT-ből
      const userId = req.user.userId;
 
      const query = new GetMeQuery(userId);
      const user = await this.getMeQueryHandler.handle(query);
 
      res.status(200).json({
        message: 'User retrieved successfully',
        data: user
      });
    } catch (error) {
      const status = error.message.includes('not found') ? 404 : 500;
      res.status(status).json({ error: error.message });
    }
  }
 
  /**
   * GET /api/users - Összes user lekérése (protected)
   */
  async getAll(req, res) {
    try {
      const query = new GetAllUsersQuery();
      const users = await this.getAllUsersQueryHandler.handle(query);
 
      res.status(200).json({
        message: 'Users retrieved successfully',
        data: users,
        count: users.length
      });
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  }
 
  /**
   * GET /api/users/:id - User lekérése ID alapján (protected)
   */
  async getById(req, res) {
    try {
      const { id } = req.params;
      const userId = parseInt(id);
 
      if (isNaN(userId)) {
        return res.status(400).json({ error: 'Invalid user ID' });
      }
 
      const query = new GetUserByIdQuery(userId);
      const user = await this.getUserByIdQueryHandler.handle(query);
 
      res.status(200).json({
        message: 'User retrieved successfully',
        data: user
      });
    } catch (error) {
      const status = error.message.includes('not found') ? 404 : 500;
      res.status(status).json({ error: error.message });
    }
  }
 
  /**
   * PUT /api/users/me - User profil frissítése (protected)
   */
  async updateMe(req, res) {
    try {
      const userId = req.user.userId;
      const { name } = req.body;
 
      const command = new UpdateUserProfileCommand(userId, name);
      const user = await this.updateUserProfileCommandHandler.handle(command);
 
      res.status(200).json({
        message: 'Profile updated successfully',
        data: user
      });
    } catch (error) {
      const status = error.message.includes('required') ? 400 : 500;
      res.status(status).json({ error: error.message });
    }
  }
}
 
module.exports = UserController;